using System.Collections;
using System.Linq;

namespace System.Windows.Controls
{
    internal static class ItemsSourceHelper
    {
        public static void InserItemInSource(this IEnumerable itemsSource, int index, object item)
        {
            if (itemsSource == null) return;
            var dataInSource = itemsSource.Cast<object>().Skip(index).Take(1).FirstOrDefault();
            if (item == dataInSource) return;
            if (!itemsSource.CanInsert(item)) return;
            itemsSource.Insert(index, item);
        }

        public static void RemoveItemFromSource(this IEnumerable itemsSource, int index, object item)
        {
            var dataInSource = itemsSource.Cast<object>().Skip(index).Take(1).FirstOrDefault();
            if (dataInSource != item) return;
            itemsSource.Remove(dataInSource);
        }
    }
}